package com.baizhi.cmfz.service.impl;

import com.baizhi.cmfz.dao.GuruDao;
import com.baizhi.cmfz.entity.Guru;
import com.baizhi.cmfz.service.GuruService;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
@Transactional
public class GuruServiceImpl implements GuruService {
    @Autowired
    private GuruDao guruDao;

    @Override
    public Map selectByPageGuru(int page, int limit, String name) {
        Map map = new HashMap();
        int offset = (page - 1) * limit;

        if (name != null && !name.equals("")) {

            name = "%" + name + "%";
        } else {
            name = null;
        }
        List<Guru> gurus = guruDao.selectAllGuru(offset, limit, name);
        int count = guruDao.selectTotalCountGuru(name);
        map.put("data", gurus);
        map.put("count", count);
        map.put("code", 0);
        map.put("msg", "");
        return map;
    }

    @Override
    public Map insertGuru(Guru guru) {
        Map map = new HashMap();

        int count = guruDao.insertGuru(guru);
        if (count > 0) {
            map.put("isInsert", true);
        } else {
            map.put("isInsert", false);
        }
        return map;
    }

    @Override
    public Map updateState(Integer id, Integer status) {
        Map map = new HashMap();
        int count = guruDao.updateState(id, status);
        if (count > 0) {
            map.put("isStatus", true);
        } else {
            map.put("isStatus", false);
        }
        return map;
    }

    @Override
    public Map updateGuru(Guru guru) {
        Map map = new HashMap();

        int count = guruDao.updateGuru(guru);
        if (count > 0) {
            map.put("isUpdate", true);
        } else {
            map.put("isUpdate", false);
        }
        return map;
    }

    @Override
    public Guru selectOneGuru(Integer id) {
        return guruDao.selectOneGuru(id);
    }

    @Override
    public Map deleteGuru(Integer id) {
        Map map = new HashMap();
        int count = guruDao.deleteGuru(id);
        if (count > 0) {
            map.put("isDelete", true);
        } else {
            map.put("isDelete", false);
        }
        return map;
    }

    @Override
    public Map insertCountGuru(InputStream inputStream) throws IOException {
        int countStatus=0;
        //2.通过poi处理流
        HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
        //3.取出文件对象中的数据
//        3.1取出表getSheetAt()通过下标获取
        HSSFSheet sheetAt = workbook.getSheetAt(0);
//        3.2获取表中的行getLastRowNum
        int lastRowNum = sheetAt.getLastRowNum();
        for (int i = 1; i < lastRowNum; i++) {
//            3.3获取行
            HSSFRow row = sheetAt.getRow(i);
//            3.4通过行得到单元格
            String id = row.getCell(0).getStringCellValue();
            String name = row.getCell(1).getStringCellValue();
            String img = row.getCell(2).getStringCellValue();
            String nickName = row.getCell(3).getStringCellValue();
            //将获取的数据封装成对象
            Guru guru = new Guru();
            guru.setGuruName(name);
            guru.setGuruNickName(nickName);
            guru.setGuruImage(img);

//            3.5添加数据库
            System.out.println("=========" + guru + "=====");
            int count = guruDao.insertGuru(guru);
            countStatus++;
        }
        System.out.println(countStatus+"----------------------"+(lastRowNum-1));
        Map map=new HashMap();
        if(countStatus==(lastRowNum-1)){
            map.put("isInsert",true);
            System.out.println("zehgn ");
        }else{
            map.put("isInsert",false);
            System.out.println("加");
        }
        return map;
    }

    @Override
    public HSSFWorkbook downloadCountGuru(int[] ids) throws IllegalAccessException {
        List<Guru> gurus = guruDao.selectConditionGuru(ids);


        String[] titltName = {"编号", "姓名", "法号", "状态"};
        //1.创建excel文件
        HSSFWorkbook workbook = new HSSFWorkbook();
        //2.创建一个工作表
        HSSFSheet sheet = workbook.createSheet("demo");
        //3.创建行对象
        HSSFRow row = sheet.createRow(0);
        for (int i = 0; i < titltName.length; i++) {
            //第一个单元格
            HSSFCell cell = row.createCell(i);
            //一个单元格对应一个标题
            cell.setCellValue(titltName[i]);
        }


        System.out.println(gurus);

        for (int i = 0; i < gurus.size(); i++) {
            Guru guru = gurus.get(i);
            HSSFRow row1 = sheet.createRow(i + 1);
//            HSSFCell cell = row1.createCell(0);
//            cell.setCellValue(guru.getGuruId());
//            row1.createCell(1).setCellValue(guru.getGuruName());
//            row1.createCell(2).setCellValue(guru.getGuruNickName());
//            row1.createCell(3).setCellValue(guru.getGuruStatus());
            //.1获取类对象
            Class<? extends Guru> guruClass = guru.getClass();
//            2获取属性对象数组
            Field[] fields = guruClass.getDeclaredFields();
//            3.遍历数组  获取属性的值  以及单元格
            for (int j = 0; j < fields.length; j++) {
//                3.1获取属性对象
                Field field = fields[j];
//                3.2获取属性的值
                field.setAccessible(true);
                String value = field.get(guru).toString();
//                3.3创建单元格
                HSSFCell cell = row1.createCell(j);
//                3.给单元格赋值
                cell.setCellValue(value);
            }

        }
        //4.创建单元格
        //HSSFCell cell = row.createCell(0);
        //5.给单元格写值
        // cell.setCellValue("单元格数据");
        //6.保存 导出文件
        return  workbook;
    }
}